package com.tsh.erp.service.impl;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import kiven.jee.dao.IBaseDao;
import kiven.jee.service.impl.BaseService;
import kiven.jee.utils.ip.IPUtil;

import com.tsh.erp.dao.SystemLoginLogDao;
import com.tsh.erp.dao.UserDao;
import com.tsh.erp.model.User;
import com.tsh.erp.service.UserService;

@Service("userService")
public class UserServiceImpl extends BaseService<User, String> implements
		UserService {
	// 该方法中若要做其他处理，则用userDao 去实现
	private UserDao userDao;

	@Resource(name = "systemlogDao")
	private SystemLoginLogDao systemlogDao;

	@Resource(name = "userDao")
	@Override
	public void setBaseDao(IBaseDao<User, String> userDao) {
		this.userDao = (UserDao) userDao;
		this.baseDao = userDao;
	}

	public User login(String userid, String password) {
		return userDao.login(userid, password);
	}

	public User login(String userid, String password, HttpServletRequest request) {
		User user = login(userid, password);
		String ip = IPUtil.getRequestIpAddr(request);
		String ipName = IPUtil.getRequestIpAddrName(request);

		if (user != null) {// 根据条件查询部位空
			// 密码被锁住
			if (user.getAccountlocked() == 1 || "0".equals(user.getEnabled())) {// 用户锁定或未激活
				if (StringUtils.isBlank(user.getLoginip())) {
					user.setLoginip(ip);
				}

				if (user.getLogindate() == null) {
					user.setLogindate(new Date());
				}

				user.setLastloginip(ip);
				user.setLastlogindate(new Date());
				userDao.update(user);

				systemlogDao.updateUserLoginLog(user.getUserid(), ip,
						"用户锁定，登录失败");
			} else if (user.getPassworderrcnt() >= 5) {// 登录次数大于5
				if (StringUtils.isBlank(user.getLoginip())) {
					user.setLoginip(ip);
				}

				if (user.getLogindate() == null) {
					user.setLogindate(new Date());
				}

				user.setLastloginip(ip);
				user.setLastlogindate(new Date());
				user.setAccountlocked(1);
				userDao.update(user);

				systemlogDao.updateUserLoginLog(user.getUserid(), ip,
						"登录次数超过5次，登录失败");
			} else {// 登录成功
				if (StringUtils.isBlank(user.getLoginip())) {
					user.setLoginip(ip);
				}

				if (user.getLogindate() == null) {
					user.setLogindate(new Date());
				}

				user.setLastloginip(ip);
				user.setLastlogindate(new Date());
				user.setPassworderrcnt(0);
				userDao.update(user);

				systemlogDao.updateUserLoginLog(user.getUserid(), ip, "登录成功");
			}
			return user;
		} else {// 根据条件查询User为空
			User user2 = this.findUserByPropertyName("userid", userid);

			if (user2 != null) {
				if (user2.getPassworderrcnt() <= 5) {
					// 密码输入错误次数控制在规定的次数之内，不进行无限累加
					user2.setPassworderrcnt(user2.getPassworderrcnt() + 1);
				}
				
				// 获得系统参数 获得错误几次记锁定该用户
				if (user2.getPassworderrcnt() > 5
						&& user2.getAccountlocked() == 0) {
					user2.setAccountlocked(1);
				}

				userDao.update(user2);
				// 写入登录日志
				systemlogDao.updateUserLoginLog(user2.getUserid(), ip,
						"密码输入错误，登录失败");
			} else {
				systemlogDao.updateUserLoginLog(userid, ip, "用户名错误，登录失败");
			}
			return null;
		}
	}

	@Override
	public User findUserByPropertyName(String propertyName, Object propertyValue) {
		return userDao.findUserByPropertyName(propertyName, propertyValue);
	}

}
